package com.ych.modules.common.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ych.SDK.alibaba.ossSDK.AliOssUtil;
import com.ych.modules.base.dbEntity.TSysAdvertiseEntity;
import com.ych.modules.base.service.ITSysAdvertiseService;
import com.ych.modules.common.entity.query.QueryAdvertiseEntity;
import com.ych.modules.common.service.AdvertiseService;
import com.ych.utils.UUID;
import com.ych.utils.beanToMap.BeanMapUtil;
import com.ych.utils.mysqlPageUtil.PageUtils;
import com.ych.utils.mysqlPageUtil.Query;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2021/1/4
 * Time: 15:29
 * Description:
 */
@Service("AdvertiseService")
public class AdvertiseServiceImpl implements AdvertiseService {
    @Autowired
    private ITSysAdvertiseService itSysAdvertiseService;
    @Autowired
    private AliOssUtil            aliOssUtil;

    @Override
    public void addAdvertise(TSysAdvertiseEntity advertiseEntity) {
        advertiseEntity.setAdId(UUID.randomUUID());
        advertiseEntity.setCreateTime(new Date());
        advertiseEntity.setStatus("0");
        advertiseEntity.setAdImgUrl(aliOssUtil.getRelativePath(advertiseEntity.getAdImgUrl()));
        if (StringUtils.isNotBlank(advertiseEntity.getVideoUrl())) {
            advertiseEntity.setVideoUrl(aliOssUtil.getRelativePath(advertiseEntity.getVideoUrl()));
        }
        itSysAdvertiseService.insert(advertiseEntity);
    }

    @Override
    public void modifyAdvertise(TSysAdvertiseEntity advertiseEntity) {
        TSysAdvertiseEntity entity = itSysAdvertiseService.selectById(advertiseEntity.getAdId());
        if (null == entity) {
            throw new RuntimeException("广告不存在!");
        }
        itSysAdvertiseService.updateById(advertiseEntity);
    }

    @Override
    public PageUtils listAdvertise(QueryAdvertiseEntity entity) {
        Wrapper<TSysAdvertiseEntity> wrapper = new EntityWrapper<TSysAdvertiseEntity>()
                .where("1=1")
                .eq(StringUtils.isNotBlank(entity.getStatus()), "status", entity.getStatus())
                .eq(StringUtils.isNotBlank(entity.getType()), "value", entity.getType())
                .eq(StringUtils.isNotBlank(entity.getUrlType()), "url_type", entity.getUrlType())
                .orderBy("create_time asc");
        Page<TSysAdvertiseEntity> page = itSysAdvertiseService.selectPage(new Query<TSysAdvertiseEntity>(BeanMapUtil.toPageQuery(entity)).getPage(), wrapper);
        List<TSysAdvertiseEntity> list = new ArrayList<>();
        for (TSysAdvertiseEntity adEntity : page.getRecords()) {
            adEntity.setAdImgUrl(aliOssUtil.getOssObjectDownAuthUrl(adEntity.getAdImgUrl()));
            list.add(adEntity);
        }
        page.setRecords(list);
        return new PageUtils(page);
    }

    @Override
    public TSysAdvertiseEntity getAdvertise(String adId) {
        TSysAdvertiseEntity entity = itSysAdvertiseService.selectById(adId);
        if (null == entity) {
            throw new RuntimeException("广告不存在!如有疑问请联系管理员!");
        }
        return entity;
    }

    @Override
    public void removeAd(String adId) {
        itSysAdvertiseService.deleteById(adId);
    }

}
